package com.szqhyxj.youxiangju.biz.creditCardEvaluation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.biz.creditCardEvaluation.buycreditcardEvaluation.BuyCreditCardEvaluationActivity;
import com.szqhyxj.youxiangju.biz.creditCardEvaluation.evaluationRecord.EvaluationRecordActivity;
import com.szqhyxj.youxiangju.biz.creditCardEvaluation.productexample.ProductexampleActivity;
import com.szqhyxj.youxiangju.biz.register.RegisterActivity;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.picker.OptionPicker;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.PickerUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CustomAdapt;

/**
 * Created by leung on 2018/11/2.
 */

public class CreditCardEcaluationActivity extends BaseActivity implements CreaditCardEcaluationContract.View, NavigationBar.NavigationBarListener, CustomAdapt {

    CreaditCardEcaluationPresenter presenter;
    @BindView(R.id.creditcard)
    TextView creditcard;
    @BindView(R.id.creditcardicon)
    ImageView creditcardicon;
    @BindView(R.id.phone_evaluation)
    TextView phoneEvaluation;
    @BindView(R.id.name_creditCardEcaluation)
    TextView nameCreditCardEcaluation;
    @BindView(R.id.getsms)
    TextView getsms;
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.agree_evaluation)
    CheckBox agreeEvaluation;
    @BindView(R.id.inquire)
    Button inquire;
    @BindView(R.id.sms)
    EditText sms;
    @BindView(R.id.user_contract_area)
    LinearLayout userContractArea;
    @BindView(R.id.valuationrecord)
    Button valuationrecord;
    @BindView(R.id.cardid)
    TextView cardid;
    @BindView(R.id.selectcard)
    LinearLayout selectcard;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, CreditCardEcaluationActivity.class);
        activity.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_card_evaluation);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        presenter = new CreaditCardEcaluationPresenter(this);
        presenter.setUserInfo();
        presenter.init();
        agreeEvaluation.setChecked(true);
        presenter.setAgree(true);
        creditcard.addTextChangedListener(new RegisterActivity.SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setCardId(s + "");
            }
        });

        sms.addTextChangedListener(new RegisterActivity.SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                presenter.setsms(s + "");
            }
        });
        agreeEvaluation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                presenter.setAgree(isChecked);
            }
        });
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }

    @Override
    public boolean isBaseOnWidth() {
        return true;
    }

    @Override
    public float getSizeInDp() {
        return 360;
    }


    public abstract static class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @OnClick({R.id.selectcard, R.id.getsms, R.id.inquire, R.id.valuationrecord, R.id.productexample, R.id.measurementCard})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.selectcard:
                presenter.selectcard();
                break;
            case R.id.getsms:
                presenter.acquireVerificationCode();
                break;
            case R.id.inquire:
                presenter.checkSms(sms.getText().toString());

                break;
            case R.id.valuationrecord:
                EvaluationRecordActivity.start(this);
                break;
            case R.id.productexample:
                ProductexampleActivity.start(this);
                break;
            case R.id.measurementCard:
                String path = String.format("MeasurementCard");
                WebActivity.startToH5(this, "用户授权协议", path);
                break;
        }
    }


    @Override
    public void showInfo(String s) {
        nameCreditCardEcaluation.setText(s);

    }

    @Override
    public void showCardId(String s) {
        cardid.setText(s);
    }

    @Override
    public void showCardList(final List<CardModel.Card> lists) {
       if(lists.size()==0){
           Toast.makeText(this,"没有信用卡",Toast.LENGTH_LONG).show();
           return;
       }
        String[] bs = new String[lists.size()];
        for (int i = 0; i < lists.size(); i++) {
            String bankcardnosub = lists.get(i).getBankCardNo().substring(lists.get(i).getBankCardNo().length() - 4, lists.get(i).getBankCardNo().length());
            String cardno = "(" + bankcardnosub + ")";
            bs[i] = lists.get(i).getBtName() + cardno;
        }
        PickerUtil.onOptionPicker(this, bs, "选择信用卡", new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {

                creditcard.setText(item);
                String btCode = lists.get(index).getCardId();
                String phone = lists.get(index).getCardPhone();
                phoneEvaluation.setText(lists.get(index).getCardPhone()+"");
                presenter.setPhone(lists.get(index).getCardPhone()+"");
                presenter.setCardId(btCode);
                phoneEvaluation.setText(lists.get(index).getCardPhone() + "");
                presenter.setPhone(lists.get(index).getCardPhone() + "");
                Log.e("-=============", btCode);
                creditcardicon.setVisibility(View.GONE);

            }
        });
    }

    @Override
    public void showPhoneNumberFail() {
        Toast.makeText(this, "请输入正确的手机号码！", Toast.LENGTH_LONG).show();
    }

    @Override
    public void showAcquiredVerificationCode() {
        getsms.setText("获取");
        getsms.setEnabled(true);
    }

    @Override
    public void showAcquiredVerificationCodeSuccess() {
        Toast.makeText(CreditCardEcaluationActivity.this, "获取验证码成功！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showAcquiredVerificationCodeFail(String s) {
        Toast.makeText(CreditCardEcaluationActivity.this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void enshowselecticon() {

    }

    @Override
    public void showAcquiringVerificationCode(int number) {
        getsms.setText(String.format("%ss", String.valueOf(number)));
        getsms.setEnabled(false);
    }

    @Override
    public void showinquire() {
        inquire.setBackgroundResource(R.drawable.background_btn04);
        inquire.setEnabled(true);
    }

    @Override
    public void showenInquire() {
        inquire.setBackgroundResource(R.drawable.background_btn_grey04);
        inquire.setEnabled(false);

    }

    @Override
    public void showFail(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void checkSuccess() {
        BuyCreditCardEvaluationActivity.start(this);
    }
}
